<?php
$pathToXmlFile = 'Db/users.xml';
try{
	//from file
	//$version, $encoding
	$dom = new DOMDocument();
	$dom->preserveWhiteSpace = false;
	$dom->formatOutput = true;
	
	//from  file
	$dom->load($pathToXmlFile);
	//var_dump($dom);die;
	//from string
	//$dom->loadXML(file_get_contents($pathToXmlFile));
	//var_dump($dom->saveXML());die;
	
	//from  file
	//loadHtmlFile
	
	//from string
	//loadHTML
	
	//$dom->save();
	//sava data 
	//save() (to a file)
	//saveXML() (to a string)
	/*saveHTML() (also
		to a string, but saves an HTML document instead of an XML file), and
		DomDocument:saveHTMLFile() (to a file in HTML format).*/
	
	//XPath Queries
	$xpath = new DomXPath($dom);
	
	//return DomNodeList
	$result = $xpath->query('//user/email/text()');
	//var_dump($result);die;
	foreach ($result as $email){
		/*var_dump(get_class($email));
		var_dump(get_class_methods($email));
		var_dump(get_class_vars(get_class($email)));*/
		//var_dump($email->data);//die;
		//var_dump(get_class_methods($email));die;
	}
	//die;
	/*if ($result->length > 0) {
		// Random access
		$email = $result->item(0);
		echo $email->data;
		// Sequential access
		foreach ($result as $email) {
			echo $email->data;
		}
	}*/
	
	//Modifying XML Documents
	
	$user = $dom->createElement('user');
	$user->setAttribute('id', 5);
	//var_dump($dom->saveXML($user));die;
	$firstName = $dom->createElement('firstName');
	$text = $dom->createTextNode('userFirstName');
	
	$firstName->appendChild($text);
//	var_dump($dom->saveXML($firstName));die;
	$user->appendChild($firstName);
	
	$lastName = $dom->createElement('lastName', 5);
	$user->appendChild($lastName);
	
	$email = $dom->createElement('email', 'myEmail@email.com');
	$user->appendChild($email);
	//var_dump($dom->saveXML($user));die;
	//documentElement property, which represents the root XML node;
	$dom->documentElement->appendChild($user);
	//$dom->appendChild($user);
	//var_dump($dom->saveXML());die;
//	die;
	//Moving Data
	$result = $xpath->query("//user");
	//var_dump($result->length);die;
	//Here, we take the second user element and place it before the first.
	//$result->item(1)->parentNode->insertBefore($result->item(1), $result->item(0));
	//var_dump($dom->saveXML());die;

	//we take the first user element and place it at the end
	//$result->item(1)->parentNode->appendChild($result->item(0));
	//header('Content-type: text/xml');
	//echo $dom->saveXML();die;
	//var_dump($dom->saveXML());die;
	
	//If you wish to duplicate a node, use “DomNode::cloneNode()”
	//$clone = $result->item(0)->cloneNode();
	//$result->item(1)->parentNode->appendChild($clone);
	
	//var_dump($dom->saveXML());
	
	//Modifying Data
	//$node = $xpath->query("/users/user/email/text()")->item(0);
	
	//שאילתא שקולה
	//$node = $xpath->query("//email/text()")->item(0);
	//$node->data = ucwords($node->data);
	
	//var_dump($dom->saveXML());
	
	//Removing Data
	/*There are three types of data you may want to remove from an XML docu-ment:
	 *  attributes, elements and CDATA. DOM provides a different method for
	each of these tasks:
	 DomNode::removeAttribute(),
	  DomNode::removeChild() and
	DomCharacterData::deleteData():*/
	$xml = <<<XML
<xml>
<text type="misc">some text here</text>
<text type="misc">some more text here</text>
<text type="misc">yet more text here</text>
</xml>
XML;
	//header('Content-type: text/xml');
	//echo $xml;die;
	$dom = new DOMDocument();
	$dom->loadXML($xml);
	$xpath = new DomXpath($dom);
	$result = $xpath->query("//text");
	$result->item(0)->parentNode->removeChild($result->item(0));
	$result->item(1)->removeAttribute('type');
	//$result = $xpath->query('text()', $result->item(2));
	//var_dump($result->item(0)->data);die;
	//header('Content-type: text/xml');
	//echo $dom->saveXML();die;
	/*
	 * Remove a range of characters from the node
	 * @link http://www.php.net/manual/en/domcharacterdata.deletedata.php
	 * @param offset int <p>
	 * The offset from which to start removing.
	 * </p>
	 * @param count int <p>
	 * The number of characters to delete. If the sum of 
	 * offset and count exceeds
	 * the length, then all characters to the end of the data are deleted.
	 * </p>
	 * @return void 
	 
	public function deleteData ($offset, $count) {}*/
	//$result->item(0)->deleteData(0, $result->item(0)->length);
	//var_dump($dom->saveXML());
	
	/*XML CDATA
	 * All text in an XML document will be parsed by the parser.
	 * But text inside a CDATA section will be ignored by the parser.
	 * But text inside a CDATA section will be ignored by the parser.
	 * A CDATA section starts with "<![CDATA[" and ends with "]]>":*/
	
	//Interfacing with SimpleXML
	//dom_import_simplexml($node);
	
/*	$sxml = simplexml_load_file($pathToXmlFile);
	
	$node = dom_import_simplexml($sxml);
	
	$dom = new DomDocument();
	$dom->importNode($node, true);
	$dom->appendChild($node);
	header('Content-type: text/xml');
	echo $dom->saveXML();die;
	var_dump($dom);die;
	//$dom->appendChild($node);
	var_dump($dom);die;
	
	//simplexml_import_dom($node)
	$dom->load($pathToXmlFile);
	$simObj = simplexml_import_dom($dom);
	echo $simObj->user[0]->email;*/
}catch(DOMException $e){
	var_dump($e->getMessage());
}